<template>
  <el-dialog :title="dialogTitle" :visible="visible" @close="onCancel">
    <el-form ref="formRef" :model="formData" :rules="formRules">
      <el-form-item label="部门名称" label-width="100px" prop="name">
        <el-input v-model="formData.name" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" label-width="100px" prop="code">
        <el-input v-model="formData.code" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" label-width="100px" prop="managerId">
        <el-select v-model="formData.managerId" placeholder="请选择部门负责人" style="width:100%;">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" label-width="100px" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" placeholder="1-100个字符" />
      </el-form-item>
      <el-form-item style="text-align:center;">
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button type="danger" @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addDepartmentApi, getDepartmentApi, getDepartmentDetailApi, getManagerListApi, updateDepartmentApi } from '@/api/department'
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    pNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      formData: {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人名字
        name: '', // 部门名字
        pid: ''// 部门父级部门id
      },
      formRules: {
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur' },
          {
            validator: async(rules, value, callback) => {
              let depts = await getDepartmentApi()
              // 判断是否是编辑
              if (this.formData.id) {
                depts = depts.filter(item => item.id !== this.formData.id)
              }
              // ----
              if (depts.some(item => item.code === value)) {
                callback(new Error('部门中已经有该编码了'))
              } else {
                callback()
              }
            }, trigger: 'blur'
          }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur' },
          {
            validator: async(rules, value, callback) => {
              let depts = await getDepartmentApi()
              // 判断是否是编辑
              if (this.formData.id) {
                depts = depts.filter(item => item.id !== this.formData.id)
              }
              if (depts.some(item => item.name === value)) {
                callback(new Error('已经有该部门了'))
              } else {
                callback()
              }
            }, trigger: 'blur'
          }
        ]
      },
      managerList: []
    }
  },
  computed: {
    dialogTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    async getManagerList() {
      const res = await getManagerListApi()
      this.managerList = res
    },
    async getDepartmentDetail(id) {
      const res = await getDepartmentDetailApi(id)
      this.formData = res
    },
    async onSubmit() {
      // 1. 整体校验
      await this.$refs.formRef.validate()
      // 2. 调用接口
      if (this.formData.id) { // 编辑
        await updateDepartmentApi(this.formData)
      } else {
        await addDepartmentApi({ ...this.formData, pid: this.pNodeId })
      }
      this.$message.success(`${this.formData.id ? '编辑' : '新增'}部门成功`)
      // 3. 通知父组件更新数据
      this.$emit('updateDepts')
      // 4. 重置表单
      this.onCancel()
    },
    onCancel() {
      // 手动清空数据对象
      this.formData = {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人名字
        name: '', // 部门名字
        pid: ''// 部门父级部门id
      }
      this.$refs.formRef.resetFields()
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped></style>
